package twi.query.template2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;

import twi.user.User;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class TemplateLibrary {

	private static HashMap<String, ArrayList<Template>> templateMap = null;
	private static final File templateDatabaseFile = new File("data/db/template/template.db");
	private static final String ALL_USER = "all user";
	private static final Charset charset = Charset.forName("ISO-8859-15");
	
	public static ArrayList<Template> getTemplates(User user){
		ArrayList<Template> out = new ArrayList<Template>();
		readDatabase();
		if (templateMap.containsKey(ALL_USER))
			out.addAll(templateMap.get(ALL_USER));
		if (templateMap.containsKey(user.getName()))
			out.addAll(templateMap.get(user.getName()));		
		return out;
	}
	public static void saveTemplate(twi.query.template2.Template template, String name,
			String desc, boolean forAll, User user) {
		try{
			if (templateMap == null)
				readDatabase();
		}catch (Exception e) {
			templateMap = new HashMap<String, ArrayList<Template>>();
		}
			
		template.setName(name);
		template.setDescription(desc);
		String userName = ALL_USER;
		if (!forAll)
			userName = user.getName();
		if (!templateMap.containsKey(userName))
			templateMap.put(userName, new ArrayList<Template>());
		templateMap.get(userName).add(template);
		XStream xs = new XStream(new DomDriver(charset.name()));
		if (!templateDatabaseFile.getParentFile().exists())
			templateDatabaseFile.getParentFile().mkdirs();
		try {			
			FileOutputStream fos = new FileOutputStream(templateDatabaseFile);
//			Writer w = new OutputStreamWriter(fos, charset);
//			String xml = xs.toXML(templateMap);
////			xml = xml.replace("&#x0;", "");
//			w.write(xml);
//			w.close();
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(templateMap);
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void readDatabase() {
		if (!templateDatabaseFile.exists()){
			templateMap = new HashMap<String, ArrayList<Template>>();
			return ;
		}
		XStream xs = new XStream(new DomDriver(charset.name()));
		
		StringBuffer buffer = new StringBuffer();
	    try {
	    	FileInputStream fin = new FileInputStream(templateDatabaseFile);
//			Reader r = new InputStreamReader(fin, charset);
//			templateMap = (HashMap<String, ArrayList<Template>>) xs.fromXML(r);
//			r.close();
	    	ObjectInputStream ois = new ObjectInputStream(fin);
	    	templateMap = (HashMap<String, ArrayList<Template>>) ois.readObject();
	    	ois.close();
			fin.close();
	    } catch (IOException e) {
	    	e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
